package com.gzw.shop.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.TintTypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gzw.shop.R;

/**
 * Created by JACKYGUO on 2017-03-10.
 * <p>
 * 自定义数字加减控件
 * <p>
 * 1.输入框只能是数字，且不能通过键盘输入
 * 2.通过加减按钮操作数字
 * 3.监听加减按钮
 * 4.数字有最小值最大值的限制
 */

public class NumAddSubView extends LinearLayout implements View.OnClickListener {

    private LayoutInflater mInflater;

    private Button btn_add;
    private Button btn_sub;
    private TextView tv_num;

    private int value;
    private int minvalue;
    private int maxvalue;

    private OnButtonClickListener mListener;

    public void setListener(OnButtonClickListener listener) {
        mListener = listener;
    }

    public NumAddSubView(Context context) {
        this(context, null);
    }

    public NumAddSubView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumAddSubView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mInflater = LayoutInflater.from(context);

        initView();

        //获取自定义属性
        if (attrs != null) {
            TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.NumAddSubView, defStyleAttr, 0);

            int val = a.getInt(R.styleable.NumAddSubView_value, 0);
            setValue(val);

            int minval = a.getInt(R.styleable.NumAddSubView_minValue, 0);
            setMinvalue(minval);

            int maxval = a.getInt(R.styleable.NumAddSubView_maxValue, 0);
            setMaxvalue(maxval);

            Drawable drawableBtnAdd = a.getDrawable(R.styleable.NumAddSubView_butAddBackGround);
            Drawable drawableBtnSub = a.getDrawable(R.styleable.NumAddSubView_butSubBackGround);
            Drawable drawabletv = a.getDrawable(R.styleable.NumAddSubView_tvBackGround);
            setBtnAddBackground(drawableBtnAdd);
            setBtnSubBackground(drawableBtnSub);
            setTvBackground(drawabletv);
            a.recycle();
        }
    }

    private void initView() {

        View view = mInflater.inflate(R.layout.widet_num_add_sub, this, true);
        btn_add = (Button) view.findViewById(R.id.btn_add);
        btn_sub = (Button) view.findViewById(R.id.btn_sub);
        tv_num = (TextView) view.findViewById(R.id.tv_num);

        btn_add.setOnClickListener(this);
        btn_sub.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_add:
                numAdd();
                if (mListener != null) {
                    mListener.onButtonAddClick(view, value);
                }

                break;
            case R.id.btn_sub:
                numSub();
                if (mListener != null) {
                    mListener.onButtonSubClick(view, value);
                }
                break;
        }
    }

    public interface OnButtonClickListener {

        void onButtonAddClick(View view, int value);

        void onButtonSubClick(View view, int value);
    }


    //增加一个
    private void numAdd() {

        if (value < maxvalue) {
            value++;
        }
        tv_num.setText(value + "");


    }

    //减少一个
    private void numSub() {

        if (value > minvalue) {
            value--;
        }
        tv_num.setText(value + "");

    }


    public int getValue() {
        String val = tv_num.getText().toString();
        if (!TextUtils.isEmpty(val)) {
            this.value = Integer.parseInt(val);
        }
        return value;
    }

    public void setValue(int value) {
        tv_num.setText(value + "");
        this.value = value;
    }

    public int getMinvalue() {
        return minvalue;
    }

    public void setMinvalue(int minvalue) {
        this.minvalue = minvalue;
    }

    public int getMaxvalue() {
        return maxvalue;
    }

    public void setMaxvalue(int maxvalue) {
        this.maxvalue = maxvalue;
    }

    public void setBtnAddBackground(Drawable drawable) {
        if (btn_add != null) {
            btn_add.setBackground(drawable);
        }
    }

    public void setBtnSubBackground(Drawable drawable) {
        if (btn_sub != null) {
            btn_sub.setBackground(drawable);
        }
    }

    public void setTvBackground(Drawable drawable) {
        if (tv_num != null) {
            tv_num.setBackground(drawable);
        }
    }
}
